<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexController extends Controller
{
    //跳转页面
    public function index($limit = 6,$olimit = 4)
    {
    	//取出热销单品
    	$data = DB::table('goods')
    			-> where('is_on_sale',1)
                -> where('is_hot',1)
    			-> limit($limit)
                -> orderBy('sort_num')
    			-> get();
        //取出一楼前4条数据
        $beforeone = DB::table('goods')
                -> where('is_on_sale',1)
                -> where('goods_name','like','%拯救者%')
                -> limit($olimit)
                -> orderBy('sort_num')
                -> get();
        //取出一楼后4条数据
        $afterone = DB::table('goods')
                -> where('is_on_sale',1)
                -> where('goods_name','like','%小新%')
                -> limit($olimit)
                -> orderBy('sort_num')
                -> get();
        //取出2层前4条数据
        $beforetwo = DB::table('goods')
                -> where('is_on_sale',1)
                -> where('goods_name','like','%E%')
                -> limit($olimit)
                -> orderBy('sort_num')
                -> get();
        //取出2层后4条数据
        $aftertwo= DB::table('goods')
                -> where('is_on_sale',1)
                -> where('goods_name','like','%S5%')
                -> limit($olimit)
                -> orderBy('sort_num')
                -> get();
        // dd($afterone);
        //取出1级分类
        $cateone = DB::table('category')
                -> where('pid',0)
                -> get();
        //取出二级分类
        $catetwo = array();

        foreach($cateone as $v)
        {
            $catetwo[] = DB::table('category')
                        -> where('pid',$v -> id)
                        -> get();
        }

        // dd($catetwo);
        //取出三级分类
        $catethree = array();
        foreach( $catetwo as $k => $v)
        {
            foreach($v as $k1 => $v1)
            {
                $catethree[] = DB::table('category')
                            -> where('pid', $v1 -> id)
                            -> get();
            }
        }
        // dd($catethree);
    	return view('home.index.index',['data' => $data,'beforeone' => $beforeone,'afterone' => $afterone,'beforetwo' => $beforetwo,'aftertwo' => $aftertwo,'cateone' => $cateone,'catetwo' => $catetwo,'catethree' => $catethree]);
	}
}
